package cn.edu.cxcyzx.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.cxcyzx.algorithm.CalProgress_project;
import cn.edu.cxcyzx.entity.Members;
import cn.edu.cxcyzx.entity.Progress_project;
import cn.edu.cxcyzx.entity.Student;
import cn.edu.cxcyzx.entity.Task;
import cn.edu.cxcyzx.entity.TaskMembers;
import cn.edu.cxcyzx.entity.TaskPOJO;
import cn.edu.cxcyzx.entity.TaskProject;
import cn.edu.cxcyzx.service.TaskService;
import cn.edu.cxcyzx.supervisor.DaoFit;
import cn.edu.cxcyzx.supervisor.ResultDo;
@Service("TaskService")
public class TaskServiceImpl implements TaskService {
	
	@Resource
	public DaoFit daoFit;

	@Override
	public ResultDo<Map<Task,List<Student>>> getProTask(int projectId) {
		// TODO Auto-generated method stub
		ResultDo<Map<Task,List<Student>>> resultDo=new ResultDo<Map<Task,List<Student>>>();
		HashMap<Task, List<Student>> hashMap=new HashMap<>();
		List<Task> tasks=daoFit.getTaskDao().getProTask(projectId);
		for (Task task : tasks) {
			List<Student> stuents=daoFit.getTaskMembersDao().getMembers(task.getTaskId());
			hashMap.put(task, stuents);
		}
		if(!hashMap.isEmpty()){
			resultDo.setResult(hashMap);
			resultDo.setSuccess(true);
		}else{
			resultDo.setSuccess(false);
		}
		return resultDo;
	}

	@Override
	public void insertTask(List<Task> tasks) {
		// TODO Auto-generated method stub
		if(tasks!=null&&tasks.size()>0){
			daoFit.getTaskDao().insertTask(tasks);
		}
		System.out.println("插入任务:"+tasks.size());
		
	}
	
	@Override
	public void updateTask(String taskId, String stuId) {
		// TODO Auto-generated method stub
		String stuName=daoFit.getStudentDao().getStuName(stuId);
		System.out.println("stuName:"+stuName);
		daoFit.getTaskDao().updateTask(taskId,stuName, stuId);
		
		int projectId=daoFit.getTaskDao().getTaskBy_taskId(taskId).getProjectId();
		String groupId=daoFit.getGroupDao().getGroupBy_projectId(projectId).getGroupId();
		Members members=new Members(stuId, stuName, groupId,taskId, 1);
		daoFit.getMembersDao().updateMember(members);
	}

	@Override
	public void insertOneTask(Task task,String[] members) {
		// TODO Auto-generated method stub
		if(task!=null){
			String groupId=daoFit.getGroupDao().getGroupBy_projectId(task.getProjectId()).getGroupId();
			task.setGroupId(groupId);
			//插入 task表
			daoFit.getTaskDao().insertOneTask(task);
			//插入members表
			if(members.length>0){
				List<TaskMembers> list=new ArrayList<>(members.length);//创建初始化指定大小的集合
				for (String id : members) {
					list.add(new TaskMembers(task.getTaskId(), id, groupId));
					daoFit.getMembersDao().updateStatus(groupId, id);
				}
				daoFit.getTaskMembersDao().insertMembers(list);
				daoFit.getTaskDao().update_taskStatus(task.getTaskId(), 2);
			}
//			if(task.getStuId()!=null&&!"".equals(task.getStuId().trim())){
//				String groupId=daoFit.getGroupDao().getGroupBy_projectId(task.getProjectId()).getGroupId();
//				Members members=new Members(task.getStuId(), task.getStuName(), groupId, task.getTaskId(), 1);
//				daoFit.getMembersDao().updateMember(members);
//			}
		}
		
	}

	@Override
	public String get_taskId() {
		// TODO Auto-generated method stub
		Integer maxId=daoFit.getTaskDao().get_maxId();
		String taskId="task"+(201800+(maxId==null?0:maxId)+1);
		return taskId;
	}

	@Override
	public ResultDo<List<TaskProject>> getTaskProject(String stuId) {
		// TODO Auto-generated method stub
		
		ResultDo<List<TaskProject>> resultDo =new ResultDo<>();
		System.out.println("stuId:"+stuId);
		List<TaskProject> list=daoFit.getTaskDao().getTaskBy_stuId(stuId);
		if(list!=null){
			System.out.println("list.size:"+list.size());
			resultDo.setSuccess(true);
			resultDo.setResult(list);
		}else{
			resultDo.setSuccess(false);
		}
		return resultDo;
	}

	@Override
	public int getTask_num(String stuId) {
		// TODO Auto-generated method stub
		return daoFit.getTaskDao().getTask_num(stuId);
	}
	
	@Transactional
	@Override
	public ResultDo<Integer> update_process(String taskId,int projectId) {
		// TODO Auto-generated method stub
		ResultDo<Integer> resultDo=new ResultDo<>();
		int process=daoFit.getTaskDao().getProcess(taskId);
		System.out.println("process:"+process);
		if(process==100){
			resultDo.setSuccess(false);
			resultDo.setMessage("任务已完成");
		}else if(process>90){
			resultDo.setSuccess(false);
			resultDo.setMessage("数据错误!");
		}else{
			//更新任务进度
			this.daoFit.getTaskDao().update_process(taskId);
			process=daoFit.getTaskDao().getProcess(taskId);//更新后的进度
			System.out.println("更新后任务进度process："+process);
			//新增项目进度
			System.out.println("progress:"+daoFit.getTaskDao().getProgress(projectId).length);
			double pro_progress=CalProgress_project.getProgress(daoFit.getTaskDao().getProgress(projectId));
			Progress_project progress_project=new Progress_project(projectId, pro_progress, "");
			System.out.println("progress_project:"+progress_project.toString());
			daoFit.getProgress_projectDao().insertRecord(progress_project);
			
			resultDo.setSuccess(true);
			resultDo.setResult(new Integer(process));
			if(process==100){
				//任务完成
				daoFit.getTaskDao().update_taskStatus(taskId, 3);
				//项目的任务量
				int taskNum=daoFit.getTaskDao().getTaskNumBy_projectId(projectId);
				//项目任务的完成量
				int taskNum_finish=daoFit.getTaskDao().getNum_taskFinish(projectId);
				if(taskNum==taskNum_finish){
					//所有任务已完成
					daoFit.getProjectDao().update_projectStatus(projectId, 8);//代码完成
					//跟新项目进度表
					daoFit.getProgress_projectDao().insertRecord(new Progress_project(projectId, CalProgress_project.TASKS_FINISH, "已完成所有任务"));
				}
			}
			
		}
		return resultDo;
	}

	@Override
	public ResultDo<List<TaskPOJO>> getAllTaskBy_leaderId(String leaderId) {
		// TODO Auto-generated method stub
		ResultDo<List<TaskPOJO>> resultDo=new ResultDo<>();
		List<TaskPOJO> list=daoFit.getTaskDao().getAllTaskBy_leaderId(leaderId);
		if(list!=null){
			resultDo.setSuccess(true);
			resultDo.setResult(list);
		}else{
			resultDo.setSuccess(false);
		}
		return resultDo;
	}

	@Override
	public void deleteTask(String taskId) {
		// TODO Auto-generated method stub
		System.out.println("删除任务："+taskId);
		daoFit.getTaskDao().deleteTask(taskId);
	}

	@Override
	public ResultDo<List<Task>> getTasks(int projectId) {
		// TODO Auto-generated method stub
		ResultDo<List<Task>> resultDo=new ResultDo<>();
		List<Task> list=daoFit.getTaskDao().getProTask(projectId);
		if(list!=null&&list.size()>0){
			resultDo.setResult(list);
			resultDo.setSuccess(true);
		}else{
			resultDo.setSuccess(false);
		}
		return resultDo;
	}

	


}
